<?php
namespace app\shop\controller;
use app\admin\model\ShopType;
use app\AdminRest;
use app\appclass\model\ClassRecord;
use app\base\model\BaseConfig;
use app\commission\model\Water;
use app\Common\LongbingServiceNotice;
use app\Common\model\LongbingActionLog;
use app\material\model\BrochureIm;
use app\material\model\File;
use app\member\model\DiscountGoods;
use app\member\model\Goods;
use app\member\model\Member;
use app\member\model\StoredOrder;
use app\shop\info\PermissionShop;
use app\shop\model\AdminGoodsParameterValue;
use app\shop\model\AdminParameter;
use app\shop\model\AdminParameterTemplate;
use app\shop\model\AdminParameterTemplateInfo;
use app\shop\model\AdminShopOrderGoods;
use app\shop\model\GoodsWholesale;
use app\shop\model\IndexSellingWater;
use longbingcore\wxcore\Socket;
use longbingcore\wxcore\SocketSend;
use longbingcore\wxcore\WxSetting;
use Redis;
use think\App;
use think\db\Connection;
use think\Exception;
use think\facade\Cache;
use think\facade\Db;
use think\Request;
use app\shop\model\AdminGoods as GoodsModel;
use app\shop\model\AdminShopSpe;
use app\shop\model\AdminShopSpePrice;
use app\shop\model\AdminCompany;
use app\shop\model\AdminShopType;
use app\shop\model\AdminCompanyGoods;
use app\shop\model\AdminShopCollage;
use think\facade\Config;
use MongoDB\Driver\Manager;




class AdminShopGoods extends AdminRest{

    protected $model;
    protected $spe_model;
    protected $spe_price_model;
    protected $company_model;
    protected $type_model;
    protected $company_goods_model;
    protected $parameter_model;
    protected $template_model;
    protected $template_info;
    protected $goods_parameter;

    public function __construct(App $app) {
        $this->app = $app;
        parent::__construct($app);
        $this->model           = new GoodsModel();
        $this->spe_model       = new AdminShopSpe();
        $this->spe_price_model = new AdminShopSpePrice();
        $this->company_model   = new AdminCompany();
        $this->type_model      = new AdminShopType();
        $this->company_goods_model = new AdminCompanyGoods();
        $this->parameter_model = new AdminParameter();
        $this->template_model  = new AdminParameterTemplate();
        $this->template_info   = new AdminParameterTemplateInfo();
        $this->goods_parameter = new AdminGoodsParameterValue();
    }





    public function randFloat($min=0, $max=1){

        $a =  $min + mt_rand()/mt_getrandmax() * ($max-$min);

        return round($a,2);
    }
    /**
     * 获取商品列表
     */
    public function goodsList(){


        $input= $this->_input;

        $name = $this->_input['name'];

        $cate = $this->_input['cate'];

        $company = $this->_input['company'];

        $dis[]= ['uniacid','=',$this->_uniacid];

        if(!empty($name)){

            $dis[] = ['name','like',"%".$name."%"];
        }

        if(!empty($cate)){

            $dis[] = ['type','=',$cate];
        }
        //自提
        if(!empty($input['is_self'])){

            $dis[] = ['is_self','=',$input['is_self']];
        }
        //快递
        if(!empty($input['is_send'])){

            $dis[] = ['is_send','=',$input['is_send']];

        }

        $sale_type      = $input['type']==2?0:1;

        $sale_out_goods = $this->spe_price_model->getSellOut($this->_uniacid,$sale_type);

        switch ($input['type']){

            case 1:
                $dis[] = ['status','=',1];
                break;

            case 2:
                $dis[]= ['status','>',-1];
                break;

            case 3:
                $dis[] = ['status','=',0];

                break;
        }

        if(!empty($company)){

            $goods_id = $this->company_goods_model->where(['uniacid'=>$this->_uniacid,'company_id'=>$company,'status'=>1])->column('goods_id');
            //公共商品id
            $public_goods_id = $this->model->where(['uniacid'=>$this->_uniacid,'public_goods'=>1])->column('id');
            //商品id拼接
            $new_goods_ids   = array_merge($goods_id,$public_goods_id);
            //获取交集
            $sale_out_goods  = array_intersect($sale_out_goods,$new_goods_ids);

        }

        $dis[] = ['id','in',$sale_out_goods];

        $data = $this->model->goodList($dis,$this->_input['limit']);

        $order_goods_model = new AdminShopOrderGoods();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){
                //总库存
                $v['all_stock'] = $this->spe_price_model->getGoodsStock($v['id']);
                //规格
                $v['spe_info']  = $this->goodsSpeList($v['id']);

                $v['true_sale_count'] = $order_goods_model->getTrueSaleNum($v['id'],$this->_uniacid);

            }
        }

        //销售中
        $data['sale_ing'] = $this->model->saleIngCount($this->_uniacid);
        //售罄
        $data['sale_out'] = $this->model->saleIngCount($this->_uniacid,2);
        //下架
        $data['sale_end'] = $this->model->saleIngCount($this->_uniacid,3);

        return $this->success($data);
    }



    public function test(){
        $index = new \app\dynamic\controller\IndexPayResunt($this->app);

        $rest = $index->qr_notify([],[]);
        dump($rest);exit;
        return $rest;
    }


    /**
     * 上下架删除商品
     */
    public function goodsStatusUpdate(){

        $input= $this->_input;

        if(isset($input['status'])){

            $data = ['status'=>$input['status']];

        }else{

            $data = ['index_show'=>$input['index_show']];

        }
        $res  = $this->model->goodsUpdateStatus($data,$input['id']);

        return $this->success($res);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-08-26 10:09
     * @功能说明:列表页修改商品库存
     */
    public function updateSpe(){

        $input= $this->_input;

        $input= $input['stock'];

        if(!empty($input)&&!is_array($input)){

            $this->errorMsg('数据错误');
        }

        foreach ($input as $value){

            $this->spe_price_model->goodsSpePriceUpdate(['id'=>$value['id']],['stock'=>$value['stock']]);
        }

        return $this->success(true);

    }


    /**
     * 修改商品
     */
    public function goodsUpdate(){

        $input= $this->_input;
        //会员折扣模型
        $discount_model = new DiscountGoods();

        $dis  = is_array($input['id'])? ['id','in',$input['id']]:['id' =>$input['id']];

        if(count($input)>2){

            $discount_data = $input['memberForm']['discount_data'];

            Db::startTrans();

            $data = $this->model->returnData($input);
            //判断商品名字长度
            if(strlen($data['name'])>110){

                $this->errorMsg('商品名字不得超过110字符，当前字符'.strlen($data['name']));
            }

            if(isset($data['is_limit'])&&$data['is_limit']==2&&$data['start_min']>$data['limit']){

                $this->errorMsg('起购数不能大于限购');

            }

            $this->model->goodsUpdate($dis,$data);
            //判断是否是全选商品
            $all_company = $input['all_company'];

            if(!$all_company){

                $company_id = $this->company_goods_model->changeData($input['basicform']['company_id']);

                $this->companyGoodsAdd($company_id,$input['id']);
            }
            $this->spe_model->goodsSpeUpdate(['uniacid'=>$this->_uniacid,'goods_id'=>$input['id']],['status'=>-1]);

            $this->spe_price_model->goodsSpePriceUpdate(['uniacid'=>$this->_uniacid,'goods_id'=>$input['id']],['status'=>-1]);

            $spe_arr = $this->goodsSpeAdd($input['specsForm']['specsItem'],$input['id']);

            $res     = $this->goodsSpePriceAdd($input['specsForm']['specsTable'],$input['id'],$spe_arr);

            if($data['is_parameter']==1 && empty($input['parameter'])){

                return $this->error('请添加商品参数');
            }
            //商品参数详情新增
            if($data['is_parameter']==1){

                $res = $this->goodsParameterValueAdd($input['parameter'], $input['id']);
            }
            $this->checkCollage($input['id']);
            //会员折扣
            $discount_model->goodsAdd($discount_data,$this->_uniacid,$input['id']);

            Db::commit();
        }else{
            $res['goods_info']    = $this->model->goodsInfo($dis);

            $res['spe_info']      = $this->goodsSpeList($this->_input['id']);

            if($res['goods_info']['public_goods']==1){
                $res['company_goods'] = $this->company_goods_model->getCompanyIds([
                    'uniacid' => $this->_uniacid,
                    'status'  => 1,
                ]);
            }else{
                $res['company_goods'] = $this->company_goods_model->getId([
                    'a.uniacid' => $this->_uniacid,
                    'a.goods_id'=> $this->_input['id'],
                    'a.status'  => 1,
                    'b.status'  => 1,
                ]);
            }
            //会员商品信息
            $res['memberForm']['is_discount'] = $res['goods_info']['is_discount'];

            $res['memberForm']['is_member']   = $res['goods_info']['is_member'];
            //商品折扣信息
            $res['memberForm']['discount_data'] = $discount_model->goodsDiscount($this->_input['id']);
            //查询商品的参数信息
            if($res['goods_info']['is_parameter']==1 && $res['goods_info']['template_id'] > 0){

                $res['parameter'] = $this->template_model->templateInfo(['id' =>$res['goods_info']['template_id']],$res['goods_info']['id'] ??0);
            }else{
                $res['parameter'] = null;
            }

        }
        return $this->success($res);
    }

    /**
     * 添加商品
     */
    public function goodsAdd(){

        $input = $this->_input;

        $discount_data = $input['memberForm']['discount_data'];

        $data  = $this->model->returnData($input);
        //判断商品名字长度
        if(strlen($data['name'])>110){

            $this->errorMsg('商品名字不得超过110字符，当前字符'.strlen($data['name']));
        }

        if(isset($data['is_limit'])&&$data['is_limit']==2&&$data['start_min']>$data['limit']){

            $this->errorMsg('起购数不能大于限购');

        }

        $data['uniacid'] = $this->_uniacid;

        Db::startTrans();

        $count    = $this->model->goodsCount(['uniacid'=>$this->_uniacid]);
        //By.jingshuixian
        //修改为行业版数量限制方式
        $permission = new PermissionShop($this->_uniacid) ;

        $addNumber = $permission->getAddGoodsNumber();

        if($addNumber !=0 &&  $addNumber <= $count){
            return $this->error('添加商品已到上限');
        }
        $goods_id = $this->model->goodsAdd($data);

        $spe_arr  = $this->goodsSpeAdd($input['specsForm']['specsItem'],$goods_id);

        $spe_res  = $this->goodsSpePriceAdd($input['specsForm']['specsTable'],$goods_id,$spe_arr);
        //会员折扣模型
        $discount_model = new DiscountGoods();
        //会员折扣
        $discount_model->goodsAdd($discount_data,$this->_uniacid,$goods_id);
        //判断是否是全选商品
        $all_company = $input['all_company'];
        if(!$all_company){
            $company_id = $this->company_goods_model->changeData($input['basicform']['company_id']);
            $res      = $this->companyGoodsAdd($company_id,$goods_id);
        }else{
            $res = true;
        }
        if($data['is_parameter']==1 && empty($input['parameter'])){
            return $this->error('请添加商品参数');
        }
        //商品参数详情新增
        if($data['is_parameter']==1){
            $res =$this->goodsParameterValueAdd($input['parameter'],$goods_id);
        }
        Db::commit();
        return $this->success($res);
    }



    /**
     * @param $data
     * @param $goods_id
     * 添加company_goods
     */
    public function companyGoodsAdd($data,$goods_id){
        $this->company_goods_model->companyGoodsDel(['uniacid'=>$this->_uniacid,'goods_id'=>$goods_id],$data);
        foreach ($data as $v){
            $com = $this->company_goods_model->companyGoodsFind(['goods_id'=>$goods_id,'status'=>1,'company_id'=>$v,'uniacid'=>$this->_uniacid]);
            if(empty($com)){
                $res = $this->company_goods_model->companyGoodsAdd(['uniacid'=>$this->_uniacid,'goods_id'=>$goods_id,'company_id'=>$v]);
            }
        }
        return !empty($res)?$res:1;
    }


    /**
     * @param $data
     * @param $goods_id
     * @return array
     * 添加多规格
     */
    public function goodsSpeAdd($data,$goods_id){
        /**
         * 循环判断是否存在多个规格名添加图片的问题
         * @date 2020/5/14 10:42 --lichuanming
         */
        $isimg_count = 0;
        foreach ($data as $item ){
            if($item['is_img'] == 1){
                $isimg_count++;
            }
        }
        if($isimg_count > 1){
            return $this->error('当前已有规格名添加了图片，继续添加则需取消上一个规格名的图片勾选');
        }

        $arr = array();
        if(!empty($data)){
            foreach ($data as $v){
                $is_img = $v['is_img']?$v['is_img']:0; #@date 2020/5/14 10:42 --lichuanming
                if(strlen($v['pid'])>10){
                    $pid =  $this->spe_model->goodsSpeAdd(['uniacid'=>$this->_uniacid,'goods_id'=>$goods_id,'title'=>$v['title'],'is_img'=>$is_img]);
                }else{
                    $this->spe_model->goodsSpeUpdate(['id'=>$v['id']],['status'=>1,'title'=>$v['title'],'is_img'=>$is_img]);
                    $pid = $v['id'];
                }
                if(!empty($v['cate'])){
                    foreach ($v['cate'] as $value){
                        $image = $is_img?$value['image']:''; #@date 2020/5/14 10:42 --lichuanming

                        if(strlen($value['id'])>10) {
                            $id = $this->spe_model->goodsSpeAdd(['uniacid' => $this->_uniacid, 'goods_id' => $goods_id, 'title' => $value['title'], 'pid' => $pid,'is_img'=>$is_img,'image'=>$image]);
                        }else{
                            $this->spe_model->goodsSpeUpdate(['id'=>$value['id']],['status'=>1,'title'=>$value['title'],'is_img'=>$is_img,'image'=>$image]);
                            $id = $value['id'];
                        }
                        $arr[$value['id']] = $id;
                    }
                }
            }
        }
        return $arr;
    }


    /**
     * @param $data
     * @param $good_id
     * @param $arr
     * @return int|string
     * 添加多规格价格
     */
    public function goodsSpePriceAdd($data,$good_id,$arr){
        if(!empty($data)){
            foreach ($data as $v){
                if (strlen($v['price'])>8||strlen($v['original_price'])>8){

                    $this->errorMsg('价格最多8位');

                }

                $pid    = explode(',',$v['id']);
                $spe_id = [];
                if(empty($arr)){
                    $this->errorMsg('规格不正确,请删除错误规格，重新编辑');
                }
                foreach ($pid as $value){
                    if(!key_exists($value,$arr)){
                        $this->errorMsg('规格不正确,请删除错误规格，重新编辑');
                    }
                    $spe_id[] = $arr[$value];
                }
                $spe_price_id   = implode('-',$spe_id);

                $ins['uniacid'] = $this->_uniacid;

                $ins['goods_id']= $good_id;

                $ins['spe_id_1']= $spe_price_id;

                $ins['price']   = $v['price'];

                $ins['stock']   = $v['stock'];

                $ins['status']  = 1;
                //原价
                $ins['original_price'] = !empty($v['original_price'])?$v['original_price']:0;
                //成本价
                $ins['cost_price'] = !empty($v['cost_price'])?$v['cost_price']:0;

                $spe_price = $this->spe_price_model->singeSpePrice(['goods_id'=>$good_id,'spe_id_1'=>$spe_price_id,'uniacid'=>$this->_uniacid]);

                if(empty($spe_price)) {

                    $res = $this->spe_price_model->goodsSpePriceAdd($ins);

                }else{

                    $res = $this->spe_price_model->goodsSpePriceUpdate(['id'=>$spe_price['id']],$ins);
                }
            }
        }
        return !empty($res)?$res:1;
    }

    /**
     * 获取商品规格
     */
    public function goodsSpeList($goods_id){

        $dis['goods_id'] = $goods_id;

        $dis['status']   = 1;

        $data['text']    = $this->spe_model->goodsSpe($dis);

        $data['price']   = $this->spe_price_model->goodsSpePrice($dis);

        if(!empty($data['price'])){

            foreach ($data['price'] as &$v){

                $v['title'] = $v['spe_name_text'];

                $v['true_id'] = $v['id'];

                $v['id']   = implode(',',$v['spe_array_text']);

            }
        }
        return $data;
    }

    /**
     *
     * 获取部门和分类
     */

    public function companyAndCate(){
        $dis[] = ['uniacid','=',$this->_uniacid];
        $data['cate']    = $this->type_model->catSortSelect($dis);
        $dis[] = ['status' ,'=',1];
        $data['company'] = $this->company_model->companySelect($dis);
        return $this->success($data);
    }

    /**
     *
     * 获取部门和分类
     */

    public function companyAndCateV2(){

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status' ,'=',1];

        $data['cate']    = $this->type_model->catSortSelect($dis);

        $data['company'] = $this->company_model->companySelect($dis);

        return $this->success($data);
    }
    /**
     *
     *检查拼团
     */
    public function checkCollage($goods_id){
        $collage  = AdminShopCollage::where(['goods_id'=>$goods_id,'status'=>1])->column('spe_price_id');
        if(!empty($collage)){
            $dis[] = ['id','in',$collage];
            $dis[] = ['status','=',1];
            $spe   = AdminShopSpePrice::where($dis)->column('id');
            if(empty($spe)){
                $this->model->goodsUpdate(['id'=>$goods_id],['is_collage'=>0]);
            }else{
                $di[] = ['spe_price_id','not in',$spe];
            }
            $di[] = ['goods_id','=',$goods_id];
            $di[] = ['status','=',1];
            AdminShopCollage::where($di)->update(['status'=>-1]);
        }
        return true;
    }


    /**
     * @author lxj
     * @DataTime: 2020/5/18 13:20
     * @功能说明:商品参数列表
     */
    public function goodsParameter(){
        $where[]= ['uniacid','=',$this->_uniacid];
        $where[]= ['status','>',-1];
        $where[]= ['pid','=',$this->_input['pid']??0];
        $data = $this->parameter_model->getList($where,$this->_input['limit']??10);
        return $this->success($data);
    }

    /**
     * @author lxj
     * @DataTime: 2020/5/19 10:54
     * @功能说明:下拉选择参数数组
     */
    public function getParameterList(){
        $where[]= ['uniacid','=',$this->_uniacid];
        $where[]= ['status','=',1];
        $where[]= ['pid','=',$this->_input['pid']??0];
        $data = $this->parameter_model->getAdminList($where);
        return $this->success($data);
    }

    /**
     * @author lxj
     * @DataTime: 2020/5/18 14:34
     * @功能说明:商品参数新增
     */
    public function goodsParameterAdd(){
        $input = $this->_input;
        $data  = $this->parameter_model->returnData($input);
        $data['uniacid'] = $this->_uniacid;
        $res = $this->parameter_model->parameterAdd($data);
        if(!$res){
            return $this->error('参数名称重复');
        }
        return $this->success($res);
    }

    /**
     * @author lxj
     * @DataTime: 2020/5/18 14:47
     * @功能说明:商品参数修改
     */
    public function goodsParameterEdit(){
        $input= $this->_input;
        $where  =['id' =>$input['id']];
        if(count($input)>2){
            $data = $this->parameter_model->returnData($input);
            $data['uniacid'] = $this->_uniacid;
            $res  = $this->parameter_model->parameterUpdate($input['id'],$data,2);
            if(!$res){
                return $this->error('参数名称重复');
            }
        }else{
            $res['info']    = $this->parameter_model->parameterInfo($where);
        }
        return $this->success($res);
    }

    /**
     * @author lxj
     * @DataTime: 2020/5/18 17:19
     * @功能说明:修改商品参数状态
     */
    public function goodsParameterStatus(){
        $input= $this->_input;
        switch ($input['status']) {
            case -1:
                //判断参数是否可以删除
                $where2 = ['parameter_id'=>$input['id'],'uniacid'=>$this->_uniacid,'status'=>1];
                $use = $this->goods_parameter->parameterUse($where2);
                if($use){
                    return  $this->error('该参数正在使用，无法删除！');
                }else{
                    //删除模板下的参数和下级参数
                    $delAll = $this->parameter_model->delParameter($input['id'],$this->_uniacid);
                }
                break;
            case 0:
                //判断是否可以禁用
                $where2 = ['parameter_id'=>$input['id'],'uniacid'=>$this->_uniacid,'status'=>1];
                $isUse = $this->template_info->isUse($where2);
                if($isUse){
                    return  $this->error('该参数正在使用，无法执行操作！');
                }
                break;
        }
        $data = ['status'=>$input['status']];
        $res  = $this->parameter_model->parameterUpdate($input['id'],$data,1);
        return $this->success($res);
    }

    /**
     * @author lxj
     * @DataTime: 2020/5/18 14:48
     * @功能说明:参数模板表列表
     */
    public function goodsParameterTemplate(){
        $where[]= ['uniacid','=',$this->_uniacid];
        $where[]= ['status','>',-1];
        $data = $this->template_model->getList($where,$this->_input['limit']??10);
        return $this->success($data);
    }

    /**
     * @author lxj
     * @DataTime: 2020/5/19 11:24
     * @功能说明:下拉模板数据
     */
    public function parameterTemplateList(){
        $where[]= ['uniacid','=',$this->_uniacid];
        $where[]= ['status','=',1];
        $data = $this->template_model->adminTemplateList($where);
        return $this->success($data);
    }

    /**
     * @author lxj
     * @DataTime: 2020/5/18 17:40
     * @功能说明:商品参数模板新增
     */
    public function goodsParameterTemplateAdd(){
        $input = $this->_input;
        $data  = $this->template_model->returnData($input);
        $data['uniacid'] = $this->_uniacid;
        if(empty($input['data'])) return $this->error('模块内容不能为空');
        Db::startTrans();
        try {
            //模板主数据存储
            $template_id   = $this->template_model->templateAdd($data);
            if(!$template_id){
                Db::rollback();
            }
            //模板详情存储
            foreach($input['data'] as $key=>$val){
                $id =  $this->template_info->templateInfoAdd(['uniacid'=>$this->_uniacid,'template_id'=>$template_id,'parameter_id'=>$val['firstId'],'pid'=>0]);
                if(!$id){
                    Db::rollback();
                }
                foreach ($val['secondList'] as $key2 =>$val2){
                    if($val2['id']){
                        $id2 =  $this->template_info->templateInfoAdd(['uniacid'=>$this->_uniacid,'template_id'=>$template_id,'parameter_id'=>$val2['id'],'pid'=>$val['firstId']]);
                        if(!$id2){
                            Db::rollback();
                        }
                    }
                }
            }
            Db::commit();
            return $this->success('');
        }catch (\Exception $e){
            Db::rollback();
            return $this->error($e);
        }
    }

    /**
     * @author lxj
     * @DataTime: 2020/5/18 17:44
     * @功能说明:商品参数模板修改
     */
    public function goodsParameterTemplateEdit(){
        $input= $this->_input;
        $where  =['id' =>$input['id']];
        if(count($input)>2){
            if(empty($input['data'])) return $this->error('模块内容不能为空');
            $data = $this->template_model->returnData($input);
            Db::startTrans();
            try {
                //模板主数据修改
                $res  = $this->template_model->templateUpdate($where,$data);
                //旧模板详情删除
                $re = $this->template_info->templateInfoDel([['uniacid','=',$this->_uniacid],['template_id','=',$input['id']],['status','>',-1]]);
                //模板详情存储
                foreach($input['data'] as $key=>$val){
                    $id =  $this->template_info->templateInfoAdd(['uniacid'=>$this->_uniacid,'template_id'=>$input['id'],'parameter_id'=>$val['firstId'],'pid'=>0]);
                    if(!$id){
                        Db::rollback();
                    }
                    foreach ($val['secondList'] as $key2 =>$val2){
                        if($val2['id']){
                            $id2 =  $this->template_info->templateInfoAdd(['uniacid'=>$this->_uniacid,'template_id'=>$input['id'],'parameter_id'=>$val2['id'],'pid'=>$val['firstId']]);
                            if(!$id2){
                                Db::rollback();
                            }
                        }
                    }
                }
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();
                return $this->error($e);
            }
        }else{
            $res    = $this->template_model->templateInfo($where,0);
        }
        return $this->success($res);
    }

    /**
     * @author lxj
     * @DataTime: 2020/5/22 20:03
     * @功能说明:模板数据参数是否可以删除
     */
    public function goodsParameterTemplateDel(){
        $input= $this->_input;
        //判断是否可以删除该参数
        $res = $this->template_model->isDel($input['template_id'],$input['parameter_id']);
        if($res){
            return $this->success('可以删除');
        }else{
            return $this->error('该参数不能删除');
        }
    }
    /**
     * @author lxj
     * @DataTime: 2020/5/19 11:15
     * @功能说明:商品参数模板状态修改
     */
    public function goodsParameterTemplateStatus(){
        $input= $this->_input;
        $where = ['id'=>$input['id']];
        switch ($input['status']) {
            case -1:
            case 0:
                //判断是否可以进行该操作
                $where2 = ['template_id'=>$input['id'],'uniacid'=>$this->_uniacid,'is_parameter'=>1];
                $isUse = $this->model->isUse($where2);
                if($isUse){
                    return  $this->error('该模板正在使用，无法执行操作！');
                }
                break;
        }
        $data = ['status'=>$input['status']];
        $res  = $this->template_model->templateUpdate($where,$data);
        return $this->success($res);
    }

    /**
     * @author lxj
     * @DataTime: 2020/5/19 16:26
     * @功能说明:模板数据返回
     */
    public function getTemplateInfo(){
        $input= $this->_input;
        //判断是否有使用该模板的记录
        $where = ['template_id'=>$input['template_id'],'is_parameter'=>1,'uniacid'=>$this->_uniacid,'status'=>1];
        $history=  $this->model->getHistory($where);
        //有商品使用该模板，查询模板数据
        $res    = $this->template_model->templateInfo(['id' =>$input['template_id']],$history['id']??0);
        return $this->success($res);
    }

    /**
     * @param $data
     * @param $goods_id
     * @功能说明:商品参数新增
     * @author lxj
     * @DataTime: 2020/5/19 16:35
     */
    public function goodsParameterValueAdd($data,$goods_id){
        //商品原来的参数数据删除
        $this->goods_parameter->goodsParameterDel(['goods_id'=>$goods_id,'uniacid'=>$this->_uniacid,'status'=>1]);
        //模板详情存储
        foreach($data as $key=>$val){
            $com = $this->goods_parameter->goodsParameterFind(['goods_id'=>$goods_id,'status'=>1,'parameter_id'=>$val['firstId']]);
            if(empty($com)){
                $res =  $this->goods_parameter->goodsParameterAdd(['uniacid'=>$this->_uniacid,'goods_id'=>$goods_id,'parameter_id'=>$val['firstId'],'parameter_value'=>'暂无']);
            }
            if($val['secondList']){
                foreach ($val['secondList'] as $key2 =>$val2){
                    $com2 = $this->goods_parameter->goodsParameterFind(['goods_id'=>$goods_id,'status'=>1,'parameter_id'=>$val2['id']]);
                    if(empty($com2) && $val2['parameter_value']!==''){
                        $res =  $this->goods_parameter->goodsParameterAdd(['uniacid'=>$this->_uniacid,'goods_id'=>$goods_id,'parameter_id'=>$val2['id'],'parameter_value'=>$val2['parameter_value']]);
                    }
                }
            }
        }
        return !empty($res)?$res:1;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-02 09:47
     * @功能说明:diy要用的
     */
    public function goodsInfoPage(){

        $input   = $this->_param;

        $keyword = $input['keyword'];

        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1
        ];

        $where = [];

        if ($keyword) {

            $where[] = ['name', 'like', "%{$keyword}%"];
        }

        $data = Db::name('longbing_card_goods')->where($dis)->where($where)->field('name as title,id')->order('id desc')->paginate($input['page_count'])->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-29 17:14
     * @功能说明:商品批发价活动列表
     */
    public function wholesaleList(){

        $input   = $this->_param;

        $goods_sale_model = new GoodsWholesale();

        $spe_model = new AdminShopSpePrice();

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status','>',0];

        $dis[] = ['a.goods_id','=',$input['goods_id']];

        $dis[] = ['b.status','=',1];

        $data = $goods_sale_model->dataList($dis,$input['limit']);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $spe = $spe_model->dataInfo(['id'=>$v['spe_id']]);
                //规格名字
                $v['spe_name'] = !empty($spe)?$spe['spe_name_text']:'';

                $v['num_text'] = implode(',',array_column($v['range'],'num_text'));

                $v['discount_text'] = implode(',',array_column($v['range'],'discount'));

            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-29 17:21
     * @功能说明:添加商品批发价活动
     */
    public function wholesaleAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $goods_sale_model = new GoodsWholesale();

        $this->checkWholesale($input);

        $data = $goods_sale_model->dataAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-02-03 16:21
     * @功能说明:检查
     */
    public function checkWholesale($data){


        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['spe_id','=',$data['spe_id']];

//        $dis[] = ['goods_id','=',$data['goods_id']];

        $dis[] = ['status','<>',-1];

        if(!empty($data['id'])){

            $dis[] = ['id','<>',$data['id']];
        }

        $goods_sale_model = new GoodsWholesale();

        $info = $goods_sale_model->dataInfo($dis);

        if(!empty($info)){

            $this->errorMsg('已有该规格的活动了');
        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-30 09:27
     * @功能说明:批发价规则详情
     */
    public function wholesaleInfo(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $goods_sale_model = new GoodsWholesale();

        $data = $goods_sale_model->dataInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-29 17:23
     * @功能说明:编辑商品批发活动
     */
    public function wholesaleUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $goods_sale_model = new GoodsWholesale();

        $this->checkWholesale($input);

        $data = $goods_sale_model->dataUpdate($dis,$input);

        return $this->success($data);
    }

    /**
     * @author chenniang
     * @DataTime: 2021-01-29 17:23
     * @功能说明:编辑商品批发活动上下架删除
     */
    public function wholesaleStatusUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $goods_sale_model = new GoodsWholesale();

        $data = $goods_sale_model->where($dis)->update(['status'=>$input['status']]);

        return $this->success($data);
    }









}
